﻿using UnityEngine;
using UnityEditor;
using UnityEngine.UI;

namespace SoarD.Editor.UI
{
    [CustomEditor(typeof(SDParticleSystem), true)]
    [CanEditMultipleObjects]
    public class SDParticleSystemEditor : UnityEditor.Editor
    {
        SerializedProperty m_Sprite;
        SerializedProperty m_Material;
        SerializedProperty m_ShapeType;
        SerializedProperty m_StartDelay;
        SerializedProperty m_StartLifetime;
        SerializedProperty m_RandLifeRatio;
        SerializedProperty m_StartSpeed;
        SerializedProperty m_RandSpeedRatio;
        SerializedProperty m_StartSize;
        SerializedProperty m_Color;
        SerializedProperty m_MaxParticles;

        SerializedProperty m_RateOverTime;
        SerializedProperty m_RateOverDistance;
        SerializedProperty m_EmissionShape;
        SerializedProperty m_Angle;

        SerializedProperty m_Velocity;
        SerializedProperty m_RandVelocityRatio;

        SerializedProperty m_Force;
        SerializedProperty m_RandForceRatio;

        SerializedProperty m_FinalColor;

        SerializedProperty m_FinalSize;

        GUIContent m_SpriteContent;
        GUIContent m_MaterialContent;
        GUIContent m_ShapeTypeContent;
        GUIContent m_StartDelayContent;
        GUIContent m_StartLifetimeContent;
        GUIContent m_RandLifeRatioContent;

        GUIContent m_StartSpeedContent;
        GUIContent m_RandSpeedRatioContent;

        GUIContent m_StartSizeContent;
        GUIContent m_ColorContent;
        GUIContent m_MaxParticlesContent;

        GUIContent m_RateOverTimeContent;
        GUIContent m_RateOverDistanceContent;
        GUIContent m_EmissionShapeContent;
        GUIContent m_AngleContent;

        GUIContent m_VelocityContent;
        GUIContent m_RandVelocityRatioContent;

        GUIContent m_ForceContent;
        GUIContent m_RandForceRatioContent;

        GUIContent m_FinalColorContent;

        GUIContent m_FinalSizeContent;

        GUIContent m_RestartContent;

        void OnEnable()
        {
            m_SpriteContent = new GUIContent("Sprite");
            m_MaterialContent = new GUIContent("材质");
            m_ShapeTypeContent = new GUIContent("形状");

            m_StartDelayContent = new GUIContent("延迟时间");
            m_StartLifetimeContent = new GUIContent("生命时间");
            m_RandLifeRatioContent = new GUIContent("随机率");
            m_StartSpeedContent = new GUIContent("粒子速度");
            m_RandSpeedRatioContent = new GUIContent("随机率");
            m_StartSizeContent = new GUIContent("粒子大小");
            m_ColorContent = new GUIContent("粒子颜色");
            m_MaxParticlesContent = new GUIContent("最大粒子数");
            m_RateOverTimeContent = new GUIContent("单位发射率");
            m_RateOverDistanceContent = new GUIContent("发射距离间隔");
            m_EmissionShapeContent = new GUIContent("发射形状");
            m_AngleContent = new GUIContent("发射角度");
            m_VelocityContent = new GUIContent("速率");
            m_RandVelocityRatioContent = new GUIContent("随机率");
            m_ForceContent = new GUIContent("加速度");
            m_RandForceRatioContent = new GUIContent("随机率");
            m_FinalColorContent = new GUIContent("最终颜色");
            m_FinalSizeContent = new GUIContent("最终粒子大小");
            m_RestartContent = new GUIContent("预览效果");


            m_Sprite = serializedObject.FindProperty("m_Sprite");
            m_Material = serializedObject.FindProperty("m_Material");
            m_ShapeType = serializedObject.FindProperty("m_ShapeType");
            m_StartDelay = serializedObject.FindProperty("m_StartDelay");
            m_StartLifetime = serializedObject.FindProperty("m_StartLifetime");
            m_RandLifeRatio = serializedObject.FindProperty("m_RandLifeRatio");
            m_StartSpeed = serializedObject.FindProperty("m_StartSpeed");
            m_RandSpeedRatio = serializedObject.FindProperty("m_RandSpeedRatio");
            m_StartSize = serializedObject.FindProperty("m_StartSize");
            m_Color = serializedObject.FindProperty("m_Color");
            m_MaxParticles = serializedObject.FindProperty("m_MaxParticles");
            m_RateOverTime = serializedObject.FindProperty("m_RateOverTime");
            m_RateOverDistance = serializedObject.FindProperty("m_RateOverDistance");
            m_EmissionShape = serializedObject.FindProperty("m_EmissionShape");
            m_Angle = serializedObject.FindProperty("m_Angle");
            m_Velocity = serializedObject.FindProperty("m_Velocity");
            m_RandVelocityRatio = serializedObject.FindProperty("m_RandVelocityRatio");
            m_Force = serializedObject.FindProperty("m_Force");
            m_RandForceRatio = serializedObject.FindProperty("m_RandForceRatio");
            m_FinalColor = serializedObject.FindProperty("m_FinalColor");
            m_FinalSize = serializedObject.FindProperty("m_FinalSize");
        }

        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            ParticleGUI();
            RestartGUI();
            serializedObject.ApplyModifiedProperties();
        }

        protected void ParticleGUI()
        {
            GUILayout.Space(5f);
            SDParticleSystem p = target as SDParticleSystem;
            EditorGUILayout.PropertyField(m_Sprite, m_SpriteContent);
            EditorGUILayout.PropertyField(m_Material, m_MaterialContent);
            GUILayout.Space(7);
            p.showParticleSystem = InspectorHelper.BeginFoldOut("Particle System", p.showParticleSystem);
            if (p.showParticleSystem)
            {
                InspectorHelper.BeginGroup();
                InspectorHelper.BeginGroup(12);
                EditorGUILayout.PropertyField(m_ShapeType, m_ShapeTypeContent);
                InspectorHelper.EndGroup();
                InspectorHelper.BeginGroup(12);
                EditorGUILayout.PropertyField(m_StartDelay, m_StartDelayContent);
                EditorGUILayout.PropertyField(m_StartLifetime, m_StartLifetimeContent);
                EditorGUILayout.PropertyField(m_RandLifeRatio, m_RandLifeRatioContent);
                InspectorHelper.EndGroup();
                InspectorHelper.BeginGroup(12);
                EditorGUILayout.PropertyField(m_StartSpeed, m_StartSpeedContent);
                EditorGUILayout.PropertyField(m_RandSpeedRatio, m_RandSpeedRatioContent);
                InspectorHelper.EndGroup();
                InspectorHelper.BeginGroup(12);
                EditorGUILayout.PropertyField(m_StartSize, m_StartSizeContent);
                EditorGUILayout.PropertyField(m_Color, m_ColorContent);
                EditorGUILayout.PropertyField(m_MaxParticles, m_MaxParticlesContent);
                InspectorHelper.EndGroup();
                InspectorHelper.EndGroup();
            }
            GUILayout.Space(5);

            p.showEmission = InspectorHelper.BeginFoldOut("Emission", p.showEmission);
            if (p.showEmission)
            {
                InspectorHelper.BeginGroup();
                EditorGUI.indentLevel++;
                EditorGUILayout.PropertyField(m_RateOverTime, m_RateOverTimeContent);
                EditorGUILayout.PropertyField(m_RateOverDistance, m_RateOverDistanceContent);
                EditorGUI.indentLevel--;
                InspectorHelper.EndGroup();
            }
            GUILayout.Space(5);

            p.showShape = InspectorHelper.BeginFoldOut("Shape", p.showShape);
            if (p.showShape)
            {
                InspectorHelper.BeginGroup();
                EditorGUI.indentLevel++;
                EditorGUILayout.PropertyField(m_EmissionShape, m_EmissionShapeContent);
                if (p.EmissionEmissionShape == SDParticleSystem.EmissionShapeType.Sector)
                {
                    EditorGUILayout.PropertyField(m_Angle, m_AngleContent);
                }
                EditorGUI.indentLevel--;
                InspectorHelper.EndGroup();
            }

            p.showVelocityOverLifeTime = InspectorHelper.BeginFoldOut("Velocity Over Life Time", p.showVelocityOverLifeTime);
            if (p.showVelocityOverLifeTime)
            {
                InspectorHelper.BeginGroup();
                p.enableVelocity = InspectorHelper.Toggle("开启速率", p.enableVelocity, true);
                if (p.enableVelocity)
                {
                    GUILayout.Space(5);
                    InspectorHelper.BeginGroup(12);
                    EditorGUILayout.PropertyField(m_Velocity, m_VelocityContent);
                    EditorGUILayout.PropertyField(m_RandVelocityRatio, m_RandVelocityRatioContent);
                    InspectorHelper.EndGroup();
                }
                InspectorHelper.EndGroup();
            }
            p.showForceOverLifeTime = InspectorHelper.BeginFoldOut("Force Over Life Time", p.showForceOverLifeTime);
            if (p.showForceOverLifeTime)
            {
                InspectorHelper.BeginGroup();
                p.enableForce = InspectorHelper.Toggle("开启加速度(力)", p.enableForce, true);
                if (p.enableForce)
                {
                    GUILayout.Space(5);
                    InspectorHelper.BeginGroup(12);
                    EditorGUILayout.PropertyField(m_Force, m_ForceContent);
                    EditorGUILayout.PropertyField(m_RandForceRatio, m_RandForceRatioContent);
                    InspectorHelper.EndGroup();
                }
                InspectorHelper.EndGroup();
            }
            p.showSize = InspectorHelper.BeginFoldOut("Size Tween ", p.showSize);
            if (p.showSize)
            {
                InspectorHelper.BeginGroup();
                p.enableSize = InspectorHelper.Toggle("开启粒子大小变化", p.enableSize, true);
                if (p.enableSize)
                {
                    GUILayout.Space(5);
                    InspectorHelper.BeginGroup(12);
                    EditorGUILayout.PropertyField(m_FinalSize, m_FinalSizeContent);
                    InspectorHelper.EndGroup();
                }
                InspectorHelper.EndGroup();
            }
            p.showColor = InspectorHelper.BeginFoldOut("Color Tween (这个比较耗性能)", p.showColor);
            if (p.showColor)
            {
                InspectorHelper.BeginGroup();
                p.enableColor = InspectorHelper.Toggle("开启粒子颜色变化(真的使用?)", p.enableColor, true);
                if (p.enableColor)
                {
                    GUILayout.Space(5);
                    InspectorHelper.BeginGroup(12);
                    EditorGUILayout.PropertyField(m_FinalColor, m_FinalColorContent);
                    InspectorHelper.EndGroup();
                }
                InspectorHelper.EndGroup();
            }
        }

        private void RestartGUI()
        {
            EditorGUILayout.BeginHorizontal();
            {
                GUILayout.Space(EditorGUIUtility.labelWidth);
                if (GUILayout.Button(m_RestartContent, EditorStyles.miniButton))
                {
                    SDParticleSystem particleSystem = target as SDParticleSystem;
                    particleSystem.Restart();
                }
            }
            EditorGUILayout.EndHorizontal();
        }
    }
}
